﻿using System;
using System.Data;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;
using Baimasoft.Manage.SqlServerDAL;

public partial class ReportAnalysis_ForecastBusiness : System.Web.UI.Page
{
    ForecastBusinessBLL bll = new ForecastBusinessBLL();
    ForecastBusiness model = new ForecastBusiness();
    Param MyP = new Param();
    private string sEditStatus = String.Empty;
    private string sID = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        try
        {
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "2")
            {
                sID = this.Request.QueryString["ID"].ToString().Trim();
            }
        }
        catch
        {

        }
        if (!IsPostBack)
        {
            BindsltArea();
            this.txtYearsDataBind();
            try
            {
                if (sEditStatus == "2")
                {
                    DataView mydv = bll.GetList("FB_ID = '" + sID + "'");
                    sltArea.Value = mydv[0]["Area_ID"].ToString();
                    txtYears.Text = mydv[0]["Years"].ToString();
                    txtVoiceBusiness.Text = mydv[0]["VoiceBusiness"].ToString();
                    txtPDCH.Text = mydv[0]["PDCH"].ToString();
                    lblBenchmarking.Text = mydv[0]["Benchmark"].ToString();
                    txtProportion.Text = mydv[0]["Proportion"].ToString();
                    txtFactBusiness.Text = mydv[0]["FactBusiness"].ToString();
                    BindsltCategory(txtYears.Text);
                    sltCategory.Value = mydv[0]["AC_ID"].ToString() + "|" + mydv[0]["BenchMark"].ToString();
                }
            }
            catch
            {

            }
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        if (!ValidateHelper.IsDecimal(txtFactBusiness.Text.Trim(), 15, 2))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上月实际话音业务量必须是数字型（可保留两位小数）！');</script>");
            return;
        }
        if (!ValidateHelper.IsDecimal(txtVoiceBusiness.Text.Trim(), 15, 2))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('本月预测话音业务量必须是数字型（可保留两位小数）！');</script>");
            return;
        }
        if (!ValidateHelper.IsDecimal(txtPDCH.Text.Trim(), 15, 2))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('预测PDCH值必须是数字型（可保留两位小数）！');</script>");
            return;
        }
        if (!ValidateHelper.IsDecimal(txtProportion.Text.Trim(), 15, 2))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('预测月度调整比例必须是数字型（可保留两位小数）！');</script>");
            return;
        }
        model.Area_ID = int.Parse(sltArea.Value);
        model.Years = txtYears.SelectedValue.Trim();
        model.FactBusiness = Decimal.Parse(txtFactBusiness.Text);
        model.VoiceBusiness = Decimal.Parse(txtVoiceBusiness.Text);
        model.PDCH = Decimal.Parse(txtPDCH.Text);
        model.Proportion = Decimal.Parse(txtProportion.Text);
        model.AC_ID = int.Parse(sltCategory.Value.Split('|')[0]);
        model.AddDate = DateTime.Now;
        model.UpdateDate = DateTime.Now;        
        model.UpdateUser = int.Parse(Session["Baimasoft_Pers_ID"].ToString());
        if (sEditStatus == "1")
        {
            if (bll.Exists(" Area_ID='" + model.Area_ID + "' and Years='" + model.Years + "'"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该记录已经存在，请修改区域或者年月！');</script>");
                return;
            }
            if (bll.Add(model) == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功！');window.location.href='ForecastBusinessManage.aspx';</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败！');</script>");
        }
        else if (sEditStatus == "2")
        {
            if (bll.Exists(" Area_ID='" + model.Area_ID + "' and Years='" + model.Years + "' and FB_ID!='" + sID + "'"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该记录已经存在，请修改区域或者年月！');</script>");
                return;
            }
            model.FB_ID = int.Parse(sID);
            if (bll.Update(model) == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');window.location.href='ForecastBusinessManage.aspx';</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('传参错误！');</script>");
        }
    }
    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "请选择县市区域...");
        sltArea.Items[0].Value = "";
    }

    private void BindsltCategory(string sYears)
    {
        AreaCategoryBLL ac = new AreaCategoryBLL();
        sltCategory.DataTextField = "Name";
        sltCategory.DataValueField = "ID";
        sltCategory.DataSource = ac.GetSltList(sYears);
        sltCategory.DataBind();
        sltCategory.Items.Insert(0, "请选择分类...");
        sltCategory.Items[0].Value = "";
    }

    protected void btnForecast_ServerClick(object sender, EventArgs e)
    {
        BindsltCategory(Util.FormateData(txtYears.Text));
    }
    private void txtYearsDataBind()
    {
        txtYears.DataTextField = "Name";
        txtYears.DataValueField = "Name";
        txtYears.DataSource = MyP.GetCodeByParentCode("1");
        txtYears.DataBind();
        txtYears.Items.Insert(0, "请选择...");
        txtYears.Items[0].Value = "";
    }
}
